<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="userName"
					type="text"
					maxlength="11"
					placeholder="用户名/电话"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="密码"
				></wInput>
			</view>
			<wButton 
				text="登 录"
				:rotate="isRotate" 
				@click.native="startLogin()"
				class="wbutton"
			></wButton>
			
			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view>
			</view>
			
			<!-- 底部信息 -->
			<view class="footer">
				<!-- <navigator url="forget" open-type="navigate">找回密码</navigator> -->
				<view @click="findPassword">找回密码</view>
				<text>|</text>
				<view @click="register">注册账号</view>
				<!-- <navigator url="register" open-type="navigate">注册账号</navigator> -->
			</view>
		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import mqttObject from '@/mqtt/mqtt-connect-clent.js';
	import storeGlobe from "../../store"
	// import uniGrid from "@/components/uni-grid/uni-grid.vue"
	// import uniGridItem from "@/components/uni-grid-item/uni-grid-item.vue"
	import md5 from "md5"
	export default {
		data() {
			return {
				userName: uni.getStorageSync('userName'),//用户/电话'admin'
				passData: uni.getStorageSync('passData'), //密码'admin123'
				hkcUser: null,
				// passWord: "c4ca4238a0b923820dcc509a6f75849b"
				logoImage: '../../static/icon/login-logo.png',
				isRotate: false //是否加载旋转
			};
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			_this= this;
			// _this.isLogin();
		},
		methods: {
			isLogin(){
				//判断缓存中是否登录过，直接登录
				try {
					const value = uni.getStorage('userInfo');
					if (value) {
						//有登录信息
						console.log("已登录用户：",value);
						uni.setStorage('userInfo',value)
						// _this.$store.dispatch("userInfo",value); //存入状态
						uni.switchTab({
							url: '../tabbar/tabbar-3/tabbar-3',
						})
					}
				} catch (e) {
					uni.showModal({
						showCancel: false,
						content: '自动登录失败！',
						success: function(res) {}
					});
				}
			},
		    startLogin(){
				//登录
				if(_this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (!_this.userName) {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '用户名不能为空'
				    });
				    return;
				}
		        if (!_this.passData) {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码不能为空'
		            });
		            return;
		        }
				_this.isRotate=true
				setTimeout(function(){
					_this.isRotate=false
				},3000)
				 
				uni.showLoading({
					title: '登录中'
				});
								
				let requestUrl = '/login/check?USERNAME=' + _this.userName + '&PASSWORD=' + md5(_this.passData);
				// http://szhzykc.com:81/api/login/check?USERNAME=admin&PASSWORD=
				console.log('requestUrl='+requestUrl)
				_this.$utils.http.get(requestUrl, {})
					.then(res => {
						console.log(JSON.stringify(res));
						if (res.code === 200) {
							if (res.data && res.msg == "success") {
								_this.hkcUser = res.data;
								_this.hkcUser.userName = _this.userName;
								_this.hkcUser.passData = _this.passData;
								console.log(JSON.stringify(_this.hkcUser));
								// uni.setStorageSync('userInfo', userdata); //存入缓存
								uni.setStorageSync('userName',_this.userName)
								uni.setStorageSync('passData',_this.passData)
								uni.setStorageSync('uid',_this.hkcUser.uid)
								
								console.log("logNewsList=")
								
								//拉取未读列表
								_this.$utils.http
								  .post("wsmessage/list/"+_this.userName+"/0", {})
								  .then(res => {
								    if (res.code === 200) {
									if(res.data[0].list){
										let logList = [] //重置该用户的未读消息列表缓存
										for(let i=0;i< res.data[0].list.length;i++){
											logList.push(res.data[0].list[i])
											mqttObject.mqttdata.logList.push(res.data[0].list[i])
										}
										// console.log("logNewsList="+JSON.stringify(mqttObject.mqttdata.logList))
										//保存vuex全局数量
										storeGlobe.state.logLength = mqttObject.mqttdata.logList.length
										console.log(storeGlobe.state.logLength)
										//保存cookie全局
										uni.setStorageSync('logList',logList)
										uni.setStorage({
											key: 'userInfo',
											data: _this.hkcUser,
											success: function() {
												//设置matt即时聊天topic
												mqttObject.mqttsubscribe("u"+_this.hkcUser.uid);//_this.hkcUser.uid
												setTimeout(function(){
													uni.switchTab({
														url: '../tabbar/tabbar-3/tabbar-3',
													})
												},2000);
												
												console.log('登录成功!');
											}
										});
									}
								    } else {
								      console.log("请求失败");
								    }
								  })
								  .catch(err => {
								    console.log("执行失败:" + JSON.stringify(err));
								  });
								
							} else {
								uni.hideLoading();
								uni.showModal({
									showCancel: false,
									content: '登录失败！',
									success: function(res) {}
								});
							}
						} else {
							uni.hideLoading();
							uni.showModal({
								showCancel: false,
								content: '登录失败！',
								success: function(res) {}
							});
						}
					}).catch(err => {
						console.log("Response for error");
						uni.hideLoading();
						uni.showModal({
							showCancel: false,
							content: '登录失败！',
							success: function(res) {}
						});
					})
				
		    },
			register() {
				uni.navigateTo({
				    url: './register/registerOne',
				    animationType: 'pop-in',
				    animationDuration: 200
				});
			},
			findPassword() {
				uni.navigateTo({
				    url: './forget?id=2',
				    animationType: 'pop-in',
				    animationDuration: 200
				});
			},
			login_weixin() {
				//微信登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
				
			}
		},
		onLoad() {
			// _this.userName = uni.getStorage('userInfo').userName;
			// _this.passData = uni.getStorage('userInfo').passData;
			
		}
	}
</script>

<style lang="less">
	
	@import url("../../components/watch-login/css/icon.css");
	@import url("../../common/css/login/main.css");
</style>
